package com.bst.bsj.portal.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.homewaimai.common.lang.ClassLoaderUtil;

public final class XMLHelper {

    private static XMLHelper xmlhelper = new XMLHelper();

    private XMLHelper() {
    }

    public static XMLHelper getInstance() {
        return xmlhelper;
    }

    public HashMap<String, String> getInfo(String xml) throws ParserConfigurationException, SAXException, IOException {
        HashMap<String, String> info = new HashMap<String, String>();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document documnet = builder.parse(readAsInputStream(xml));
        Element root = documnet.getDocumentElement();
        NodeList nl = root.getChildNodes();
        int length = nl.getLength();
        Node node = null;
        for (int i = 0; i < length; i++) {
            node = nl.item(i);
            if (node.getNodeType() == Document.ELEMENT_NODE) {
                info.put(node.getNodeName(),  node.getNodeValue());
            }
        }
        return info;
    }
    
    

    public InputStream readAsInputStream(String str) {
        ByteArrayInputStream stream = null;
        try {
            stream = new ByteArrayInputStream(str.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return stream;
    }
    
    
    public static void main(String[] args) {
        URL[] urls = ClassLoaderUtil.whichClasses(DocumentBuilderFactory.class.getName());
        for (URL url : urls) {
            System.out.println(url);
        }
        try {
            
            String xml = "<xml><ToUserName><![CDATA[gh_06671dbe2089]]></ToUserName><FromUserName><![CDATA[oPJ2mjpSYzuheeak8lAhxSu0O67Q]]></FromUserName><CreateTime>1359112482</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[星]]></Content><MsgId>5837343661775611251</MsgId></xml>";
            xml = xml.replaceAll("<!\\[CDATA\\[", "").replaceAll("\\]\\]>","");
            System.out.println(xml);
            HashMap<String, String> params = XMLHelper.getInstance().getInfo(xml);
            System.out.println( params.get("MsgType"));
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
